import { merge, ajax, fixPercent } from '../../helper/helper';
import _ from 'underscore';
import moment from 'moment';
import staticData from '../static/index';


// 获取客流报表统计图
const getOccupy = function (type, time, dispatch) {
    let endTime = moment().format('YYYY-MM-DD'), startTime;
    switch (time) {
        case 'hour':
            startTime = moment().subtract(6, 'h').format('YYYY-MM-DD HH:MM:ss');
            endTime = moment().subtract(1, 'h').format('YYYY-MM-DD HH:MM:ss');
            break;
        case 'week':
            startTime = moment().subtract(6, 'd').format('YYYY-MM-DD');
            break;
        case 'month':
            startTime = moment().subtract(29, 'd').format('YYYY-MM-DD');
            break;
        case 'halfYear':
            startTime = moment().subtract(179, 'd').format('YYYY-MM-DD');
            break;

    }

    ajax({
        url: `/dashboard/${type}`,
        type: 'GET',
        data: { startTime, endTime },
        success: (data) => {
            try {
                data = JSON.parse(data);
            } catch (error) {

            }
            if (data.success) {
                if (type == 'traffic') {
                    dispatch({
                        type: 'set-traffic-data',
                        val: { index: 0, keyIndex: 0, name: `${staticData.timeMap[time]}客流量`, data: data.result.data }
                    });
                } else if (type == 'total') {
                    dispatch({
                        type: 'set-occupy-data',
                        val: { index: 2, keyIndex: 0, name: 'VIP客户和普通客户占比', data: data.result.data }
                    });
                } else if (type == 'gender') {
                    dispatch({
                        type: 'set-gender-data',
                        val: { index: 2, keyIndex: 1, name: '顾客性别占比', data: data.result.data }
                    });
                } else if (type == 'age') {
                    dispatch({
                        type: 'set-age-data',
                        val: { index: 1, keyIndex: 1, name: '顾客各年龄段数量', data: data.result.data }
                    });
                } else if (type == 'sequential') {
                    dispatch({
                        type: 'set-sequential-data',
                        val: { index: 3, keyIndex: 2, name: `${staticData.timeMap[time]}客流量环比`, data: data.result.data }
                    });
                } else if (type == 'vipIncrease') {
                    dispatch({
                        type: 'set-vip-increase-data',
                        val: { index: 0, keyIndex: 3, name: 'VIP客户增加对比图', data: data.result.data }
                    });
                } else if (type == 'amountAndCount') {
                    dispatch({
                        type: 'set-amount-traffic-data',
                        val: { index: 0, keyIndex: 4, name: `${staticData.timeMap[time]}销售额与客流量对比`, data: data.result.data }
                    });
                } else if (type == 'vipAmountAndCount') {
                    dispatch({
                        type: 'set-vip-amount-traffic-data',
                        val: { index: 0, keyIndex: 5, name: `${staticData.timeMap[time]}销售额与VIP增长数量对比`, data: data.result.data }
                    });
                }
            }
        }
    });

}

const topListSetPageIndex = function (dispatch, params) {
    let PageIndex = params.skip / params.limit;
    params.currentPageIndex = PageIndex;
    params.targetPageIndex = PageIndex;
    dispatch({
        type: 'top-list-set-pageIndex',
        val: PageIndex
    })
}

const topListRenderData = function (dispatch, params) {
    ajax({
        type: 'GET',
        url: `/sales/top`,
        success: (data) => {
            try { data = JSON.parse(data) } catch (err) { };
            if (data && data.success) {
                dispatch({
                    type: 'top-list-render-data',
                    val: data.result
                });

            } else {
                dispatch({
                    type: 'top-list-render-data',
                    val: { data: [] }
                })
            }
            // dispatch({
            //     type: 'set-top-data',
            //     val: { index: 3, data: [] }
            // });
        },
        error: (result) => {
            dispatch({
                type: 'top-list-render-data',
                val: {
                    data: [],
                    count: 0
                }
            })
        }
    })

    // setTimeout(function () {
    //     dispatch({
    //         type: 'top-list-render-data',
    //         val: staticData.listData
    //     });
    //     // dispatch({
    //     //     type: 'set-top-data',
    //     //     val: staticData.listData
    //     // });
    // }, 100);

}


const topListRequest = function (dispatch, params) {
    dispatch({
        type: 'top-list-request',
        val: params
    })
}

const getSalesData = function (type, time, dispatch) {
    let startTime, endTime;
    switch (time) {
        case 'hour':
            startTime = moment().subtract(6, 'h').format('YYYY-MM-DD HH:MM:ss');
            endTime = moment().subtract(1, 'h').format('YYYY-MM-DD HH:MM:ss');
            break;
        case 'week':
            startTime = moment().subtract(6, 'd').format('YYYY-MM-DD');
            endTime = moment().format('YYYY-MM-DD');
            break;
        case 'month':
            startTime = moment().subtract(29, 'd').format('YYYY-MM-DD');
            endTime = moment().format('YYYY-MM-DD');
            break;
        case 'halfYear':
            startTime = moment().subtract(179, 'd').format('YYYY-MM-DD');
            endTime = moment().format('YYYY-MM-DD');
            break;

    }
    ajax({
        type: 'GET',
        url: `/sales/${type}`,
        data: {
            startTime,
            endTime
        },
        success: data => {
            try {
                data = JSON.parse(data);
            } catch (error) {

            }
            if (data.success) {
                if (type == 'amount') {
                    dispatch({
                        type: 'set-amount-data',
                        val: { index: 0, name: `${staticData.timeMap[time]}销售额`, keyIndex: 0, data: data.result.data }
                    });
                } else if (type == 'rate') {
                    dispatch({
                        type: 'set-rate-data',
                        val: { index: 0, name: `${staticData.timeMap[time]}连带率`, keyIndex: 1, data: data.result.data }
                    });
                } else if (type == 'price') {
                    dispatch({
                        type: 'set-price-data',
                        val: { index: 0, name: `${staticData.timeMap[time]}客单价`, keyIndex: 2, data: data.result.data }
                    });
                } else if (type == 'complete') {
                    dispatch({
                        type: 'set-complete-rate-data',
                        val: { index: 0, name: `${staticData.timeMap[time]}成交率`, keyIndex: 3, data: data.result.data }
                    });
                } else if (type == 'count') {
                    dispatch({
                        type: 'set-count-data',
                        val: { index: 0, name: `${staticData.timeMap[time]}成交单数`, keyIndex: 4, data: data.result.data }
                    });
                } else if (type == 'top') {
                    data.result.data.forEach((item, index) => {
                        item.key = index;
                    })
                    dispatch({
                        type: 'set-top-list-data',
                        val: { index: 3, name: '顾客购买力TOP5', data: data.result.data }
                    });
                }
            }
        }

    });
}



export const actions = {
    getOccupy: getOccupy,
    topListSetPageIndex: topListSetPageIndex,
    topListRenderData: topListRenderData,
    topListRequest: topListRequest,
    getSalesData: getSalesData,


}